/* NIBRS weather policy merge .do file
files used: NIBRS_1991_2016.dta, US_weather_1990_2017.dta, monthly_gun_policies.dta
files created: NIBRS_Analysis.dta
programs used: merge
notes: merge on state date
*/
********************************************************************************
// merge NIBRS all years with weather and policy data

*bring in NIBRS data
use "Processed Data/NIBRS_1991_2016.dta", clear

*merge with weather data
merge m:1 fips date using "Processed Data/US_weather_1991_2016_daily.dta"
keep if _merge==3
drop _merge

*merge with concealed carry policy data
gen str_fips = string(fips, "%05.0f")
gen st_fips = substr(str_fips, 1, 2)
destring st_fips, replace


merge m:1 STATE year month using "Processed Data/monthly_gun_policies.dta"
keep if _merge==3
drop _merge

*************************
*    Restrict sample
*************************
egen tag = tag(ori9 year month)
egen distinct = total(tag), by(ori9 year)
keep if distinct == 12
drop if population == 0

**********************************
*    Create analysis variables
**********************************

** RTC variables
*More/less prohibited
gen more_prohibited = 0 if unrestricted == 1 | shall_issue == 1
replace more_prohibited = 1 if may_issue == 1 | prohibited == 1
label define binary1 0 "Less Prohibited (Unrestricted, Shall issue)" 1 "More Prohibited (May issue, Prohibited)"
label values more_prohibited binary1

gen less_prohibited = 1 if unrestricted == 1 | shall_issue == 1
replace less_prohibited = 0 if may_issue == 1 | prohibited == 1
label define binary2 1 "Less Prohibited (Unrestricted, Shall issue)" 0 "More Prohibited (May issue, Prohibited)"
label values less_prohibited binary2

gen BG = 0 
replace BG = 1 if BACKGROUND == 1
gen WAIT = 0
replace WAIT = 1 if ANY_WAIT == 1
gen MP = 0
replace MP = 1 if more_prohibited == 1

egen std_laws = std(lawtotal)

gen temp_std_laws = tMean*std_laws
gen temp_wait = tMean*ANY_WAIT
gen temp_BG = tMean*BACKGROUND
gen temp_MP = tMean*MP
gen temp_max_MP = tMax*MP
gen temp_min_MP = tMin*MP

gen prec_std_laws = prec*std_laws
gen prec_wait = prec*ANY_WAIT
gen prec_BG = prec*BG
gen prec_MP = prec*MP

**********************************
*    Create Outcome Variables 
**********************************

gen homicide_pc = homicide/population
replace homicide_pc = homicide_pc*100000

gen gun_homicide_pc = homicide_gun/population
replace gun_homicide_pc = gun_homicide_pc*100000

gen ngun_homicide_pc = homicide_ngun/population
replace ngun_homicide_pc = ngun_homicide_pc*100000

gen homicide_pc_morning = homicide_morning/population
replace homicide_pc_morning = homicide_pc_morning*100000

gen homicide_pc_afternoon = homicide_afternoon/population
replace homicide_pc_afternoon = homicide_pc_afternoon*100000

gen homicide_pc_night = homicide_night/population
replace homicide_pc_night = homicide_pc_night*100000

gen homicide_at_home_pc = homicide_at_home/population
replace homicide_at_home_pc = homicide_at_home_pc*100000

gen homicide_outside_pc = homicide_outside/population
replace homicide_outside_pc = homicide_outside_pc*100000

gen homicide_other_pc = homicide_other/population
replace homicide_other_pc = homicide_other_pc*100000

gen assault_pc = assault/population
replace assault_pc = assault_pc*100000

gen agg_assault_pc = agg_assault/population
replace agg_assault_pc = agg_assault_pc*100000

gen gun_assault_pc = assault_gun/population
replace gun_assault_pc = gun_assault_pc*100000

gen ngun_assault_pc = assault_ngun/population
replace ngun_assault_pc = ngun_assault_pc*100000

gen gun_agg_assault_pc = agg_assault_gun/population
replace gun_agg_assault_pc = gun_agg_assault_pc*100000

gen ngun_agg_assault_pc = agg_assault_ngun/population
replace ngun_agg_assault_pc = ngun_agg_assault_pc*100000

gen any_homicide = 0
replace any_homicide = 1 if homicide > 0


**********************************
*    Create fixed effects
**********************************

egen ORI = group(ori9)
gen week = week(date)
gen dow = dow(date)

egen sample_month = group(year month)
egen state_sample_month = group(state sample_month)
egen ori_sample_month = group(ori9 sample_month)

preserve
keep STATE ORI ori9 fips date year month day lon lat week dow sample_month state_sample_month ori_sample_month tMean tMean2 tMean3 tMean4 DD_H tMax tMin prec BG WAIT MP std_laws temp_std_laws temp_wait temp_BG temp_MP temp_max_MP temp_min_MP prec_std_laws prec_wait prec_BG prec_MP population homicide_pc gun_homicide_pc ngun_homicide_pc homicide_pc_morning homicide_pc_afternoon homicide_pc_night homicide_at_home_pc homicide_outside_pc homicide_other_pc agg_assault_pc gun_agg_assault_pc ngun_agg_assault_pc any_homicide week dow sample_month state_sample_month ori_sample_month

saveold "Analysis Data/NIBRS_analysis.dta", replace version(12)
restore

bys STATE: egen min_year = min(year) if MP == 0
bys STATE: egen max_year = max(year) if MP == 1

preserve 

keep if year != min_year & year != max_year

keep STATE ORI fips year month day lon lat tMean prec MP temp_MP prec_MP homicide_pc week dow sample_month state_sample_month ori_sample_month 
saveold "Analysis Data/NIBRS_analysis_donut.dta", replace version(12)

restore 

** Climates **

bys ORI: egen climate = mean(tMean)
xtile climate_terc = climate, nq(3)

gen HOT = 0
replace HOT =1 if climate_terc == 3
gen COLD = 0
replace COLD = 1 if climate_terc == 1
gen WARM = 0
replace WARM = 1 if climate_terc == 2

foreach x in HOT WARM COLD {
preserve 
drop if `x' == 1

keep STATE ORI fips year month day lon lat tMean prec temp_MP prec_MP MP homicide_pc week dow sample_month state_sample_month ori_sample_month 
saveold "Analysis Data/NIBRS_analysis_drop_`x'.dta", replace version(12)
restore 
}

** Seasons **

gen winter = 0
replace winter = 1 if inlist(month,1,2,12)

gen spring = 0
replace spring = 1 if inlist(month,3,4,5)

gen summer = 0
replace summer = 1 if inlist(month,6,7,8)

gen fall = 0
replace fall = 1 if inlist(month,9,10,11)

foreach x in winter spring summer fall {
preserve 
drop if `x' == 1

keep STATE ORI fips date year month day lon lat tMean prec temp_MP prec_MP MP homicide_pc week dow sample_month state_sample_month ori_sample_month 
saveold "Analysis Data/NIBRS_analysis_drop_`x'.dta", replace version(12)
restore 
}

** collapse to county-day

preserve 

collapse (sum) homicide population (mean) tMean prec MP, by(fips STATE date)

gen homicide_pc = homicide/population
replace homicide_pc = homicide_pc*100000

gen year = year(date)
gen month = month(date)
gen day = day(date)

egen sample_month = group(year month)
egen county_sample_month = group(fips sample_month)
egen state_sample_month = group(STATE sample_month)
gen week = week(date)
gen dow = dow(date)

gen temp_MP = tMean*MP
gen prec_MP = prec*MP

sum homicide_pc

saveold "Analysis Data/NIBRS_analysis_county_day.dta", replace version(12)
restore

gen LP = 0
replace LP = 1 if MP == 0

bys STATE: egen min_MP = min(MP) 
bys STATE: egen min_LP = min(LP) 

bys STATE: egen min_group = min(sample_month) if LP == 1
bys STATE: egen Ei = min(min_group)
bys STATE: egen min_group_yr = min(year) if LP == 1
bys STATE: egen Ei_year = min(min_group_yr)

gen calendar_time = sample_month - Ei 
gen calendar_time_year = year - Ei_year

gen calendar_time_annualized = calendar_time
replace calendar_time_annualized = calendar_time_annualized/12
replace calendar_time_annualized = floor(calendar_time_annualized)

gen never = 0
replace never = 1 if min_MP == 1
replace calendar_time_annualized = -99 if never == 1
replace calendar_time = -99 if never == 1

rename calendar_time_annualized CT_rel 
replace CT_rel = 10 if CT_rel > 9 & CT_rel != .
replace CT_rel = -7 if CT_rel < -6

replace calendar_time = -30 if calendar_time < -30 
replace calendar_time = 60 if calendar_time > 60 & calendar_time != .

preserve

keep STATE ORI fips year month day CT_rel lon lat tMean prec temp_MP prec_MP homicide_pc week dow sample_month ori_sample_month 
saveold "Analysis Data/NIBRS_analysis_ES.dta", replace version(12)

restore


** Group-time treatment effects **

replace Ei = 0 if min_MP == 1

replace Ei = . if min_LP ==1

replace Ei = . if Ei == 1

preserve

drop if Ei == .

keep homicide_pc tMean prec prec_MP temp_MP STATE ORI sample_month ori_sample_month Ei dow week
saveold "Analysis Data/NIBRS_analysis_staggered.dta", replace version(12)
restore


